package edu.kit.ksri.db.mturk.plugins.correctness;

import org.apache.log4j.Logger;

import edu.kit.ksri.db.mturk.standaloneutils.OfflinePoolManager.Worker;
import edu.kit.ksri.db.mturk.utils.messaging.Result;
import edu.kit.ksri.db.mturk.utils.messaging.ResultOutputCollector;
import edu.kit.ksri.db.mturk.utils.messaging.ResultQueue;
import edu.kit.ksri.db.mturk.var.StaticInformation;

/**
 * Copyright (c) 2010-2011 Karlsruhe Institute of Technology (KIT), Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 * 
 * --------------------------------------------------------------------------
 * 
 * Software: CSP/WMV tool for dynamic quality management of microtasks
 * http://www.ksri.kit.edu/Upload/Users/PeopleClouds/HCOMP2011_An_extendable_toolkit_for_managing_quality_of_human-based_eServices.pdf
 * 
 * Organization: Karlsruhe Institute of Technology (KIT), Germany
 * http://www.kit.edu
 * 
 * Project: PeopleClouds project of the Karlsruhe Service Research Institute(KSRI)
 * http://www.ksri.kit.edu/Default.aspx?PageId=818
 * 
 * Contributing authors: David Bermbach, Sandra Rath, Pascal Wichmann, Robert Kern
 */
public abstract class AbstractQualityControlImplementation implements Runnable {

	private static Logger basiclog = Logger
			.getLogger("basic." + AbstractQualityControlImplementation.class);
	
	/** defines whether results are processed or not */
	private static boolean running = true;

	/** is set to true once the run method has terminated */
	private static boolean terminated = false;

	public void run() {
		while (running) {
			try {
				Thread.sleep(StaticInformation.millisBetweenQueuePolling);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			for (Result r : ResultQueue.getInstance().getAllResults())
				this.onMessage(r);
		}
		basiclog.info("Terminated.");
		terminated = true;
	}

	/**
	 * While the run method pulls {@link Result} messages from the
	 * {@link ResultQueue}, the onMessage method of implementing classes is
	 * invoked for every entry. Implementations should decide whether an
	 * assignment will be accepted, whether a HIT should be extended and whether
	 * a HIT has been completed. Also, it should update the individual
	 * {@link Worker} correctness level and store the {@link Result} object in
	 * the {@link ResultOutputCollector} singleton.
	 * 
	 * @param result
	 *            an {@link Result} message to be proceessed
	 */
	protected abstract void onMessage(Result result);

	/**
	 * @return the running
	 */
	public static boolean isRunning() {
		return running;
	}

	/**
	 * @param running
	 *            the running to set
	 */
	public static void setRunning(boolean running) {
		AbstractQualityControlImplementation.running = running;
	}

	/**
	 * @return the terminated
	 */
	public static boolean isTerminated() {
		return terminated;
	}

	/**
	 * @param terminated
	 *            the terminated to set
	 */
	public static void setTerminated(boolean terminated) {
		AbstractQualityControlImplementation.terminated = terminated;
	}

}
